package com.zhou.JavaAndroidFirm.adapter;

import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;

import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.base.BaseApplication;
import com.zhou.JavaAndroidFirm.bean.FavortItem;
import com.zhou.JavaAndroidFirm.widget.CircleMovementMethod;
import com.zhou.JavaAndroidFirm.widget.FavortListView;
import com.zhou.JavaAndroidFirm.widget.NameClickable;

import java.util.List;

/**
 * @author Administrator
 * @date 2018/7/24 0024
 * @des 点赞适配器
 */
public class FavortListAdapter {
    private FavortListView mListView;
    private List<FavortItem> datas;


    public FavortListView getListView() {
        return mListView;
    }

    @NonNull
    public void bindListView(FavortListView listview) {
        if (listview == null) {
            throw new IllegalArgumentException("FavortListView is null ....");
        }
        mListView = listview;
        this.mListView = mListView;
    }

    public List<FavortItem> getDatas() {
        return datas;
    }

    public void setDatas(List<FavortItem> datas) {
        this.datas = datas;
    }

    public int getCount() {
        if (datas != null && datas.size() > 0) {
            return datas.size();
        }
        return 0;
    }

    public long getItemId(int position) {
        return position;
    }

    public Object getItem(int position) {
        if (datas != null && datas.size() > position) {
            return datas.get(position);
        }
        return null;
    }

    public void notifyDataSetChanged() {
        if (mListView == null) {
            throw new NullPointerException("listview is null, please bindListView first...");
        }

        SpannableStringBuilder builder = new SpannableStringBuilder();
        if (datas != null && datas.size() > 0) {
            builder.append(setImgSpan());
            FavortItem item = null;
            for (int i = 0; i < datas.size(); i++) {

                item = datas.get(i);
                if (item != null) {
                    builder.append(setClickableSpan(item.getUserNickname(), i));
                    if (i != datas.size() - 1) {
                        builder.append(",");
                    }
                }
            }
        }
        mListView.setText(builder);
        mListView.setMovementMethod(new CircleMovementMethod(R.color.circle_name_selector_color));
    }

    private SpannableString setClickableSpan(String userNickname, int i) {
        SpannableString subjectSpanText = new SpannableString(userNickname);
        subjectSpanText.setSpan(new NameClickable(mListView.getSpanClickListener(), i), 0, subjectSpanText.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return subjectSpanText;
    }

    private SpannableString setImgSpan() {
        String img = "  ";
        SpannableString imgSpan = new SpannableString(img);
        imgSpan.setSpan(new ImageSpan(BaseApplication.getAppContext(), R.drawable.dianzansmal, DynamicDrawableSpan.ALIGN_BASELINE)
                , 0
                , 1
                , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        return imgSpan;
    }

}
